#配置日志路径
if [ -z "$ORA_SLA_IP" ];then
    ORA_PRI_IP=$(/usr/sbin/ip a|grep `/usr/sbin/ip r|grep default|awk '{print $5}'`|grep inet|awk '{print $2}'|awk -F'/' '{print $1}')
fi
typeset SCRIPT_NAME="oracle_slave_restart_${ORA_SLA_IP}"
typeset LOG_PATH='/tmp/dr_log'
[ ! -d "${LOG_PATH}" ] && mkdir -p ${LOG_PATH}
typeset LOG_FILE="${LOG_PATH}/${SCRIPT_NAME}.log"
function log_format()
{
    echo "[`date +"%F %H:%M:%S"`] [$*]" | tee -a ${LOG_FILE}
}

if [ ! -f "${LOG_FILE}" ];then
    touch ${LOG_FILE}
    if [ $? -ne 0 ];then
        log_format "ERROR" "failed to create log file" && log_format "ERROR" "script_execute_error!" && exit 1
    else
        log_format "INFO" "success to create log file"
    fi
fi

log_format "begin to restart oracle slave................................."

#设置oracle环境变量
source ~/.bash_profile

#检查check次数
[ ! ${check_times} ] && typeset check_times='3'
expr 1 + ${check_times} &>/dev/null
if [ $? -ne 0 ];then
    log_format "ERROR" 'Argument needs to be integer' && log_format "ERROR" "script_execute_error!" && exit 1
fi

sqlplus / as sysdba <<eof  > /tmp/stop_disconnet3.log
shutdown immediate;
startup mount;
eof

> /tmp/stop_disconnet3.log
sqlplus / as sysdba <<eof  > /tmp/stop_disconnet3.log
alter database open;
alter database recover managed standby database using current logfile disconnect from session;
eof

cat /tmp/stop_disconnet3.log | tee -a ${LOG_FILE}
dis3num=`cat /tmp/stop_disconnet3.log |grep 'Database altered'|grep -v grep |wc -l`
if [ $dis3num -eq 2 ];then
    log_format "INFO" "success to finally alter database using current logfile disconnect from session"
else
    log_format "ERROR" "fail to finally alter database using current logfile disconnect from session" && log_format "ERROR" "script_execute_error!" && exit 1
fi
log_format "INFO" "script_execute_success!"